/*************************************************
* Title：TinyToolKit
* Author：Opportunity 
* Describe：绘制UI管理器的窗体
* CreateTime：2020-07-07 14:56:32
* Version：1.0
* Modify Recorder：
*************************************************/

using System;
using System.Collections.Generic;
using TinyToolKit;
using TinyToolKit.AssetLoad;
using UnityEditor;
using TinyToolKit.UI;
using UnityEngine;

namespace TinyToolKitEditor
{
    public class DrawWindowUI : DrawWindowBase   
    {
        private bool isFoldoutCurForms = true;
        private bool isFoldoutStack = true;
        private string uiFormConfigSavePath;   
        private Vector2 uiScrollPos;

        private TUIManager _uiManager;
        private TinyToolSettingAsset _toolSetting;

        public override void InitWindow(ScriptableObject setting)
        {
            _toolSetting = (TinyToolSettingAsset) setting;
        }

        protected override void DrawInternalMainGUI() 
        {
            #region Editor
            DrawEditorGUI();
            #endregion

            #region Runtime
            DrawRuntimeGUI(); 
            #endregion
        }

        private void DrawEditorGUI() 
        {
            EditorGUI.BeginDisabledGroup(EditorApplication.isPlaying);
            _toolSetting.uiFormLocalizationPathType = (UIFormPathType)TinyToolWindowUtil.DrawEnumPopup("配置表加载方式：",_toolSetting.uiFormLocalizationPathType);
            if (_toolSetting.uiFormLocalizationPathType != UIFormPathType.Custom)
                _toolSetting.uiFormsConfigResPath = EditorGUILayout.TextField("加载路径：",_toolSetting.uiFormsConfigResPath);
            else
                EditorGUILayout.HelpBox("需要调用UI管理器的 SetUIFormPaths() 方法来设置UI配置表",MessageType.Info);

            EditorGUILayout.Space();
            
            _toolSetting.clearAllFormsOnSceneChanged = EditorGUILayout.Toggle("切换场景时清除所有窗体：", _toolSetting.clearAllFormsOnSceneChanged);
            
            EditorGUI.EndDisabledGroup();
            
            TinyToolWindowUtil.DrawDefaultEditorGUI(); 
        }

        private void DrawRuntimeGUI()
        {
             if (EditorApplication.isPlaying == false || TinyTool.Instance.HasManager<TUIManager>() == false) return;
            
            if (_uiManager == null) _uiManager = TinyTool.Instance.GetManager<TUIManager>();

            Dictionary<string, UIFormBase> curDisplayUiForms = _uiManager.CurDisplayUIForm;
            if (curDisplayUiForms != null && curDisplayUiForms.Count > 0)
            {
                EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                isFoldoutCurForms = EditorGUILayout.Foldout(isFoldoutCurForms, "非模态窗体");
                if (isFoldoutCurForms)
                {
                    TinyToolWindowUtil.DrawTableHead("窗体", "状态");

                    foreach (var uiForm in curDisplayUiForms)
                    {
                        string formName = uiForm.Key;
                        string stateStr = "状态：" + Enum.GetName(typeof(UIFormStateType), uiForm.Value.GetStateType);
                        
                        EditorGUILayout.BeginHorizontal();

                        EditorGUILayout.LabelField(formName, stateStr);
                        if (GUILayout.Button("Select","minibutton",GUILayout.Width(60)))
                        {
                            string layer = Enum.GetName(typeof(UIFormLayerType), uiForm.Value.UIFormLayerType); 
                            string path = $"TinyTool/TinyCanvas/{layer}Root/{uiForm.Key}";
                            GameObject target = GameObject.Find(path);
                            Selection.activeObject = target;
                        }
                        
                        EditorGUILayout.EndHorizontal();
                    }
                }
                EditorGUILayout.EndVertical();
            }

            Stack<UIFormBase> curDisplayModalUIForms = _uiManager.CurDisplayModalUIForms;
            if (curDisplayModalUIForms != null && curDisplayModalUIForms.Count > 0)
            {
                EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                isFoldoutStack = EditorGUILayout.Foldout(isFoldoutStack, "模态窗体");
                if (isFoldoutStack)
                {
                    TinyToolWindowUtil.DrawTableHead("窗体", "状态");

                    foreach (var uiForm in curDisplayModalUIForms)
                    {
                        string formName = uiForm.GetUIFormName;
                        
                        string stateStr = "状态：" + Enum.GetName(typeof(UIFormStateType), uiForm.GetStateType);
                        if (uiForm == curDisplayModalUIForms.Peek())
                            stateStr = "【栈顶】" + stateStr; 
                        
                        EditorGUILayout.BeginHorizontal();

                        EditorGUILayout.LabelField(formName, stateStr);
                        
                        if (GUILayout.Button("Select","minibutton",GUILayout.Width(60)))
                        {
                            string layer = Enum.GetName(typeof(UIFormLayerType), uiForm.UIFormLayerType);
                            string path = $"TinyTool/TinyCanvas/{layer}Root/{uiForm.GetUIFormName}"; 
                            GameObject target = GameObject.Find(path);
                            Selection.activeObject = target;
                        }
                        
                        EditorGUILayout.EndHorizontal();
                    }
                }
                EditorGUILayout.EndVertical();
            }
        }
    }
}
